const time = require('/utils/util.js');

App({
  register_info: {
    code: null,

    //昵称
    nickname: wx.getStorageSync('reg_nickname'),
    //手机号
    telephone: wx.getStorageSync('reg_telephone'),
    //性别
    sex: wx.getStorageSync('reg_sex'),
    //头像地址
    avatar_url: wx.getStorageSync('reg_avatar_url'),
    //用户角色
    register_role: wx.getStorageSync('reg_register_role'),
    //签名
    signature: wx.getStorageSync('reg_signature'),
    //邀请码
    invite_code: wx.getStorageSync('reg_invite_code'),
    //微信Id
    wxinfo_id: wx.getStorageSync('reg_wxinfo_id'),
  },
  globalData: {
    token: wx.getStorageSync('token'),
    openId: '',
    unionId: '',
    tab_is_show: '1.6.0',
    is_show:'',

    start_date: '',
    end_date: '',
    flag: "day",


    pj_month: null,
    pj_year: null,

    sjcNow: null, //现在时间戳

    now_time: null, //现在年月日

    week: [],

    url: "https://xct.zhibohome.net/", //url

    now_month: null,

    realTime: null,
    tabbar: {
      color: "#fff",
      selectedColor: "#d0a76c",
      backgroundColor: "#1b1b1b",
      borderStyle: "black",
      list: [{
          pagePath: "../../../pages/recruit/index/index",
          text: "招聘",
          iconPath: "../../../images/icon/icon_rec.png",
          selectedIconPath: "../../../images/icon/icon_recS.png",
          selected: true
        },
        {
          pagePath: "../../../pages/industry/industry",
          text: "行业",
          iconPath: "../../../images/icon/icon_ind.png",
          selectedIconPath: "../../../images/icon/icon_indS.png",
          selected: false
        },
        {
          pagePath: "../../../pages/mine/mine",
          text: "我的",
          iconPath: "../../../images/icon/icon_mine.png",
          selectedIconPath: "../../../images/icon/icon_mineS.png",
          selected: false
        }
      ],
      position: "bottom"
    }
  },
  //切换tabbar的方法  
  editTabBar: function() {
    var tabbar = this.globalData.tabbar,
      currentPages = getCurrentPages(),
      _this = currentPages[currentPages.length - 1],
      pagePath = _this.__route__;
    (pagePath.indexOf('/') != 0) && (pagePath = '/' + pagePath);
    for (var i in tabbar.list) {
      // tabbar.list[i].selected = false;
      (tabbar.list[i].pagePath == pagePath) && (tabbar.list[i].selected = true);
    }
    _this.setData({
      tabbar: tabbar
    });
    // console.log(tabbar)
  },
  week: function(year) {
    var d = new Date(year, 0, 1);
    while (d.getDay() != 1) {
      d.setDate(d.getDate() + 1);
    }
    var to = new Date(year + 1, 0, 1);
    var i = 1;

    var str = [];
    for (var from = d; from < to;) {
      str += `第${(i > 9) ? i : '0' + i}周 ${(from.getMonth() + 1) > 9 ? (from.getMonth() + 1) : '0' + (from.getMonth() + 1)}/${(from.getDate()) > 9 ? (from.getDate()) : '0' + (from.getDate())}-`;
      from.setDate(from.getDate() + 6);
      if (from < to) {
        str += `${(from.getMonth() + 1) > 9 ? (from.getMonth() + 1) : '0' + (from.getMonth() + 1)}/${(from.getDate()) > 9 ? (from.getDate()) : '0' + (from.getDate())}*`;
        from.setDate(from.getDate() + 1);
      } else {
        to.setDate(to.getDate() - 1);
        str += `${(to.getMonth() + 1) > 9 ? (to.getMonth() + 1) : '0' + (to.getMonth() + 1)}/${(to.getDate()) > 9 ? (to.getDate()) : '0' + (to.getDate())}*`;
      }
      i++;
    }
    var arr = str.split('*')
    // console.log(arr)
    // console.log(str)

    this.globalData.week = arr;
  },

  getTab() {
    wx.request({
      url: this.globalData.url + 'api/timing/applet/show',
      method: 'POST',
      success: (e) => {
        // console.log(e)
        let is_show = e.data.data.is_show;
        this.globalData.is_show = is_show;
        if (is_show == this.globalData.tab_is_show) {
          wx.reLaunch({
            url: '../../../pages/login/login',
          })
        }else{
          // wx.reLaunch({
          //   url: '../../../pages/recruit/index/index',
          // })
        }
      }
    })
  },
  /**
   * 当小程序初始化完成时，会触发 onLaunch（全局只触发一次）
   */
  onLaunch: function() {
   
    this.getTab();
    const updateManager = wx.getUpdateManager()

    updateManager.onCheckForUpdate(function(res) {
      // 请求完新版本信息的回调
      //  console.log(res.hasUpdate)
    })

    updateManager.onUpdateReady(function() {
      wx.showModal({
        title: '更新提示',
        content: '小主~已经更新完毕了呢~',
        success: function(res) {
          if (res.confirm) {
            // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
            updateManager.applyUpdate()
          }
        }
      })

    })

    updateManager.onUpdateFailed(function() {
      // 新的版本下载失败
    })
  },

  /**
   * 当小程序启动，或从后台进入前台显示，会触发 onShow
   */
  onShow: function(options) {
    this.getTab();
    if (wx.getStorageSync('token') == '') {
      // wx.redirectTo({
      //   url: 'pages/author/author',
      // })
      console.log("token为空")
    } else {
      // console.log('zouzheli')
      let that = this;
      wx.request({
        url: this.globalData.url + 'api/mine/index',
        data: {
          // token: this.globalData.token,
          token: wx.getStorageSync('token'),
        },
        method: 'POST',
        success: function(e) {
          // console.log(e);
          //判断是否 有角色
          if ((wx.getStorageSync('reg_register_role') == "0")) {
            console.log("没有身份")
            // wx.redirectTo({
            //   url: 'pages/selectIdentity/selectIdentity',
            // })
            // console.log('跳转')
          } else {
            //正常进行

            var register_info = e.data.data.register;

            //昵称
            that.register_info.nickname = register_info.nickname;
            wx.setStorageSync('reg_nickname', register_info.nickname)
            //手机号
            that.register_info.telephone = register_info.telephone;
            wx.setStorageSync('reg_telephone', register_info.telephone)
            //性别
            that.register_info.sex = register_info.sex;
            wx.setStorageSync('reg_sex', register_info.sex)
            //头像地址
            that.register_info.avatar_url = register_info.avatar_url;
            wx.setStorageSync('reg_avatar_url', register_info.avatar_url)
            //用户角色
            that.register_info.register_role = register_info.register_role;
            wx.setStorageSync('reg_register_role', register_info.register_role)
            //签名
            that.register_info.signature = register_info.signature;
            wx.setStorageSync('reg_signature', register_info.signature)
            //邀请码
            that.register_info.invite_code = register_info.invite_code;
            wx.setStorageSync('reg_invite_code', register_info.invite_code)
            //微信Id
            that.register_info.wxinfo_id = register_info.wxinfo_id;
            wx.setStorageSync('reg_wxinfo_id', register_info.wxinfo_id)
          }
        },
      })
    }



    //进入首页初始时间
    var date = new Date();
    var nowtime = date.getFullYear() + '/' + ((date.getMonth() + 1) > 9 ? (date.getMonth() + 1) : "0" + (date.getMonth() + 1)) + '/' + (date.getDate() > 9 ? (date.getDate()) : '0' + (date.getDate()));
    var test = Date.parse(nowtime) / 1000;
    var now_month = date.getFullYear() + '-' + ((date.getMonth() + 1) > 9 ? (date.getMonth() + 1) : "0" + (date.getMonth() + 1));
    this.globalData.now_month = now_month
    this.globalData.now_time = nowtime;
    this.globalData.start_date = test;
    this.globalData.end_date = test;

    //最新时间时间戳
    var date_all = new Date();
    var sjc_now = Date.parse(date_all) / 1000
    this.globalData.sjcNow = sjc_now

    this.week(date.getFullYear())


    //  更新用户登录时间
    if (wx.getStorageSync('token')) {
      wx.request({
        method: "POST",
        url: this.globalData.url + 'api/user/update/logintime',
        data: {
          token: wx.getStorageSync('token'),
          from: "WxMiNi",
        },
        success: function(e) {
          //  console.log(e)
        }
      })
    }


    this.globalData.realTime = time.formatTimeTwo(this.globalData.start_date, 'Y年M月D日');
    // console.log(this.globalData.realTime)
    // console.log(this.globalData.start_date)


  },

  /**
   * 当小程序从前台进入后台，会触发 onHide
   */
  onHide: function() {

  },

  /**
   * 当小程序发生脚本错误，或者 api 调用失败时，会触发 onError 并带上错误信息
   */
  onError: function(msg) {

  }
})